package com.myschool.server.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.myschool.server.manager.UserManager;
import com.myschool.server.model.RegistrationStatus;
import com.myschool.server.model.School;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;
import com.myschool.server.web.command.UserSession;

@Controller
public class RegistrationController {

	@Autowired
	private UserManager userManager;

	@RequestMapping("preRegister.htm")
	public ModelAndView preRegister() {

		List<School> schools = new ArrayList<School>();
		School school = new School();
		school.setName("school1");
		school.setEntityId(11L);

		schools.add(school);
		ModelAndView map = new ModelAndView("registration", "schools", schools);
		map.addObject("userTypes", UserType.values());

		return map;
	}

	@RequestMapping("register/registerAccount.htm")
	public String registerUserToSchool(User user, HttpServletRequest request) {

		UserSession userSession = (UserSession) request.getSession(false)
				.getAttribute(UserSession.CURRENT_USER);
		System.out.println("username=" + userSession.getUserName());
		user.setEntityId(userSession.getUserId());
		try {
			this.userManager.registerUser(user);
		} catch (Exception exp) {
			return "message:Error during registration";
		}

		return "message:Registration success,Please wait for the approval from admin.";
	}

	@RequestMapping("register/preRegisterSchool.htm")
	public String preRegisterSchool(School school, User user,
			HttpServletRequest request) {

		return "registerSchool.htm";
	}

	@RequestMapping("register/registerSchool.htm")
	public String registerSchool(User user, HttpServletRequest request) {

		if (request.getSession().getAttribute(UserSession.CURRENT_USER) != null) {
			UserSession userSession = (UserSession) request.getSession(false)
					.getAttribute(UserSession.CURRENT_USER);

			System.out.println("username=" + userSession.getUserName());
			user.setEntityId(userSession.getUserId());

		} else {
			user.setRegistrationStatus(RegistrationStatus.RA);
		}
		try {
			this.userManager.registerSchoolAndUser(user);
			UserSession userSession = new UserSession(user.getEntityId(),
					user.getFirstName(), user.getType(), user.getSchool()
							.getEntityId());
			request.getSession().setAttribute(UserSession.CURRENT_USER,
					userSession);
		} catch (Exception exp) {
			exp.printStackTrace();
			return "message:Error during registration";
		}

		return "message:Registration success,Please wait for the approval from admin.";
	}

	@RequestMapping("register/activateRegistration.htm")
	public String activateRegistration(@RequestParam String key,
			@RequestParam String user, HttpServletRequest request) {

		try {
			Long userId=Long.parseLong(user);
			this.userManager.activateUser(userId,key);

		} catch (Exception exp) {
			exp.printStackTrace();
			return "message:Error during registration";
		}

		return "message:Registration success,Please wait for the approval from admin.";
	}

}
